/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.shop.viewer;

//import demo.EqcUsers;
import idea.model.qnrlibrary.EqcUsers;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import org.openide.awt.ActionReferences;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.nodes.Children;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//org.shop.viewer//UserViewer//EN",
        autostore = false)
@TopComponent.Description(
        preferredID = "UserViewerTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "explorer", openAtStartup = false)
@ActionID(category = "Window", id = "org.shop.viewer.UserViewerTopComponent")

@ActionReferences(
        {
    @ActionReference(path = "Menu/Window", position = 1133),
    @ActionReference(path = "Menu/File/Open", position = 2000)
})
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_UserViewerAction",
        preferredID = "UserViewerTopComponent")
@Messages({
    "CTL_UserViewerAction=UserViewer",
    "CTL_UserViewerTopComponent=User Viewer Window",
    "HINT_UserViewerTopComponent=This is a User Viewer window"
})
public final class UserViewerTopComponent extends TopComponent implements ExplorerManager.Provider {

    private boolean resetFields = false;
    private static ExplorerManager em = new ExplorerManager();

    public UserViewerTopComponent() {
        initComponents();
        setName(Bundle.CTL_UserViewerTopComponent());
        setToolTipText(Bundle.HINT_UserViewerTopComponent());
//        EntityManager entityManager = Persistence.createEntityManagerFactory("RussCustomerLibraryPU").createEntityManager();
//
//        //Query query = entityManager.createNamedQuery("EqcUsers.findAll");
//        //List<EqcUsers> resultList = query.getResultList();
//
//        Query q;
//        q = entityManager.createQuery("SELECT e FROM EqcUsers e order by e.uaName");
//        List<EqcUsers> resultList = q.getResultList();
//
//        //for (EqcUsers c : resultList) {
//        //jTextArea1.append(c.getUaName() + " (" + c.getDescription() + ")" + "\n");
//        //}
//
//        em.setRootContext(new UsersRootNode(Children.create(new EqcUsersChildFactory(resultList), true)));
        refreshNode();
        associateLookup(ExplorerUtils.createLookup(em, getActionMap()));

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        beanTreeView1 = new org.openide.explorer.view.BeanTreeView();

        setBackground(new java.awt.Color(70, 84, 150));
        setToolTipText(org.openide.util.NbBundle.getMessage(UserViewerTopComponent.class, "UserViewerTopComponent.toolTipText")); // NOI18N
        setAutoscrolls(true);

        beanTreeView1.setToolTipText(org.openide.util.NbBundle.getMessage(UserViewerTopComponent.class, "UserViewerTopComponent.beanTreeView1.toolTipText")); // NOI18N
        beanTreeView1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(beanTreeView1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(beanTreeView1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.openide.explorer.view.BeanTreeView beanTreeView1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return em;
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    //public static void refreshNode() {
    public void refreshNode() {
        //EntityManager entityManager = Persistence.createEntityManagerFactory("RussCustomerLibraryPU").createEntityManager();
        EntityManager entityManager = Persistence.createEntityManagerFactory("QnrLibraryPU").createEntityManager();
        Query q;
        q = entityManager.createQuery("SELECT e FROM EqcUsers e order by e.uaName");
        List<EqcUsers> resultList = q.getResultList();

        //Query query = entityManager.createNamedQuery("EqcUsers.findAll");
        //List<EqcUsers> resultList = query.getResultList();
        em.setRootContext(new UsersRootNode(Children.create(new EqcUsersChildFactory(resultList), true)));
    }

}
